<template>
    <div class="loginbody">
      <div class="logindata">
        <div class="logintext">
          <h2>网上书城</h2>
        </div>
        <div class="formdata">
          <el-form ref="form" :model="form" :rules="rules">
            <el-form-item prop="username">
              <el-input
                v-model="form.username"
                clearable
                placeholder="请输入账号"
              ></el-input>
            </el-form-item>
            <el-form-item prop="password">
              <el-input
                v-model="form.password"
                clearable
                placeholder="请输入密码"
                show-password
              ></el-input>
            </el-form-item>
            <el-form-item prop="captcha" style="height: 40px;">
              <el-input
                v-model="form.captcha"
                clearable
                placeholder="请输入验证码" style="width: 220px;"
              ></el-input>
              <img src="http://10.68.155.147:8081/bookstore_servlet/DecodeServlet" 
              alt="" id="captcha" ref="captcha" @click="img_update" style="filter: grayscale(0);">
            </el-form-item>
          </el-form>
        </div>
        <div class="tool">
          <div>
            <el-checkbox v-model="checked" @change="remenber"
              >记住密码</el-checkbox
            >
          </div>
          <div>
            <span class="shou" @click="forgetpas">忘记密码？</span>
          </div>
        </div>
        <div class="butt">
          <el-button type="primary" @click.native.prevent="login(form)"
            >登录</el-button
          >
          <el-button class="shou" @click="register">注册</el-button>
        </div>
      </div>
      <el-alert
    title="登录失败"
    type="error"
    :description="msg"
    v-if="alert_state"
    id="alert"
    show-icon>
  </el-alert>
  <h1 id="counter">实时在线人数为{{ WebCounter.counter }}  网站累计访问{{ WebCounter.totalcounter }}</h1>
    </div>
  </template>
  
  <script>
import axios from 'axios';
  export default { 
    name: "login",

    data() {
      return {
        form: {
          password: "",
          username: "",
          captcha:"",
        },
        checked: true,
        alert_state:false,
        img_state:true,
        msg:"",
        WebCounter:{
          counter:0,
          totalcounter:0,
        },
        rules: {
          username: [
            { required: true, message: "请输入用户名", trigger: "blur" },
            { max: 10, message: "不能大于10个字符", trigger: "blur" },
          ],
          password: [
            { required: true, message: "请输入密码", trigger: "blur" },
            { max: 10, message: "不能大于10个字符", trigger: "blur" },
          ],
          captcha: [
            { required: true, message: "请输入验证码", trigger: "blur" },
            { max: 4, message: "不能大于4个字符", trigger: "blur" },
          ],
        },
      };
    },
    watch:{
      checked:{
        immediate:true,
        handler(newVal,oldVal){
          if(newVal){
            if(localStorage.getItem("username")==""||localStorage.getItem("username")==null){
              localStorage.setItem("username",this.form.username)
              localStorage.setItem("password",this.form.password)
            }
            
          }else{
            localStorage.removeItem("username")
            localStorage.removeItem("password")
          }
        }
      }
    },
    mounted() {
      console.log(localStorage.getItem("username"))
      this.form.username=localStorage.getItem("username")
      this.form.password=localStorage.getItem("password")
      axios.get('/api/findCounter').then(
                response=> {
                    console.log(response.data)
                    this.WebCounter.counter=response.data.counter
                    this.WebCounter.totalcounter=response.data.totalCounter
                },
                err=> {
                  console.log(err)
                }   
              )
    },
    methods: {
      login(form) {
        console.log(form)
        this.$http.get("api/UserServlet",{
          params:{
                username:this.form.username,
                password:this.form.password,
                captcha:this.form.captcha
            }}).then(res => {
                console.log(res.data)
                if(res.data.captcha==true&&res.data.login_state==true){
                  this.$store.dispatch("accout_information/save_state",res.data);
                  this.$store.dispatch("accout_information/login",this.form);
                  this.$router.push("/table");
                }
                if(res.data.captcha==false){
                  this.msg = "验证码错误！！！"
                  this.alert_state = true
                  setTimeout(()=>{this.alert_state = false},1000);
                  this.img_update();
                }
                if(res.data.captcha==true&&res.data.login_state==false){
                  this.msg = "账户名或者密码错误！！！"
                  this.alert_state = true
                  setTimeout(()=>{this.alert_state = false},1000);
                  this.img_update();
                }
            })
    },
    img_update(){
      console.log("img_update")
      this.$refs.captcha.src = "http://10.68.155.147:8081/bookstore_servlet/DecodeServlet?time=" + Date.now()
    },
    register(){
      this.$router.push("/enroll");
    },
    forgetpas(){
      this.$router.push("/forgetpass");
    },
    remenber(){

    }
    },
    
  };
  </script>
  
  <style scoped>
  .loginbody {
    width: 100%;
    height: 100%;
    min-width: 1000px;
    background-image: url("../assets/bg.jpg");
    background-size: 100% 100%;
    background-position: center center;
    overflow: auto;
    background-repeat: no-repeat;
    position: fixed;
    line-height: 100%;
    padding-top: 150px;
  }
  
  .logintext {
    margin-bottom: 20px;
    line-height: 50px;
    text-align: center;
    font-size: 30px;
    font-weight: bolder;
    color: white;
    text-shadow: 2px 2px 4px #000000;
  }
  
  .logindata {
    width: 400px;
    height: 300px;
    transform: translate(-50%);
    margin-left: 50%;
  }
  
  .tool {
    display: flex;
    justify-content: space-between;
    color: #606266;
  }
  
  .butt {
    margin-top: 10px;
    text-align: center;
  }
  
  .shou {
    cursor: pointer;
    color: #606266;
  }
  
  /*ui*/
  /* /deep/ .el-form-item__label {
    font-weight: bolder;
    font-size: 15px;
    text-align: left;
  }
  
  /deep/ .el-button {
    width: 100%;
    margin-bottom: 10px;
  
  } */

  #captcha{
    width: 150px;
    height: 40px;
    position: relative;
    float: right;
  }

  #alert{
    position: absolute;
    height: 70px;
    width: 70%;
    top: 150px;
    left: 15%;
    font-size: 12px;
  }
  #counter {
  position: absolute;
  /* bottom: 2rem; */
  bottom: 35%;
  right: 38%;
  /* margin: 0; */
  font-size: 50px;
  color: hsl(210 40% 60%);
}
  </style>
  
  
